package com.cxl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxl.entity.User;
import com.cxl.esrepository.EsRepository;
import com.cxl.mapper.UserMapper;
import com.cxl.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 崔学磊
 * @since 2022-03-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EsRepository esRepository;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public Page<User> findAll(User user, Integer pageNum, Integer pageSize) {
        if (esRepository.count() == 0) {
            List<User> userList = userMapper.findAll(user);
            esRepository.saveAll(userList);
        }
        //es组合查询
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        //性别
        if (user.getSex() != null) {
            queryBuilder.must(QueryBuilders.matchQuery("sex", user.getSex()));
        }
        //学历
        if (user.getEid() != null) {
            queryBuilder.must(QueryBuilders.termQuery("eid", user.getEid()));
        }
        //用户名
        if (StringUtils.isNoneBlank(user.getName())) {
            queryBuilder.must(QueryBuilders.matchQuery("name.keyword", user.getName()));
        }

        //分页
        PageRequest of = PageRequest.of(pageNum - 1, pageSize);

        //高亮
        HighlightBuilder.Field name = new HighlightBuilder.Field("name").postTags("</font>").preTags("<font color='red'>");
        //不分片
        name.numOfFragments(1);

        //封装查询
        NativeSearchQuery build = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withPageable(of)
                .withHighlightFields(name)
                .build();

        //查询结果
        SearchHits<User> search = elasticsearchRestTemplate.search(build, User.class);
        ArrayList<User> arrayList = new ArrayList<>();
        List<SearchHit<User>> searchHits = search.getSearchHits();
        for (SearchHit<User> searchHit : searchHits) {
            User content = searchHit.getContent();
            if (searchHit.getHighlightField("name")!=null && searchHit.getHighlightField("name").size()>0){
                content.setName(searchHit.getHighlightField("name").get(0));
            }
            arrayList.add(content);
        }
        PageImpl<User> page = new PageImpl<User>(arrayList, of, search.getTotalHits());
        return page;
    }
}
